package cn.jsj.cxsys.dao;

import cn.jsj.cxsys.pojo.*;
import java.util.*;
import cn.jsj.cxsys.controller.packageClass.*;
import org.apache.ibatis.annotations.*;
import cn.jsj.cxsys.dao.dynamicSQL.*;

public interface UserFunction
{
    @Results(id = "toUser", value = { @Result(id = true, property = "userID", column = "user_id"), @Result(property = "teleNumber", column = "tele_number"), @Result(property = "className", column = "class_name"), @Result(property = "name", column = "name"), @Result(property = "account", column = "account"), @Result(property = "password", column = "password") })
    @Select({ "select * from vi_user where user_id=#{id} limit 1" })
    User getOneStudentById(@Param("id") int id);

    @ResultMap({ "toUser" })
    @Select({ "select * from vi_user where lader=1 and user_id=#{id} limit 1" })
    Teacher getOneTeacherById(@Param("id") final int id);

    @Select({ "select count(*) from tb_user where account=#{account}" })
    int haveAccount(@Param("account") final String account);

    @Select({ "select count(*) from tb_user where tele_number=#{teleNumber}" })
    int haveTeleNumber(@Param("teleNumber") final String teleNumber);

    @Insert({ "insert into tb_user set account=#{account}, name=#{name}, class_id=(select class_id from tb_class where class_name=#{className}),tele_number=#{teleNumber}, password=#{password}" })
    int insertOneStudent(final User student);

    @Update({ "update tb_user set lader=1 where account=#{account}" })
    int setUserToTeacher(@Param("account") final String account);

    @Select({ "select count(*) from tb_user where account=#{account} and password=#{password}" })
    int judgeLogin(@Param("account") final String account, @Param("password") final String password);

    @Select({ "select count(*) from tb_user where account=#{account} and password=#{password} and user_id=#{userID}" })
    int judgeUserPojo(final User user);

    @Select({ "select count(*) from tb_class where class_name=#{classname}" })
    int haveClassName(@Param("classname") final String classname);

    @Delete({ "delete from tb_user where account=#{account}" })
    int deleteOneUserByAccount(@Param("account") final String account);

    @Select({ "select count(*) from tb_user where account=#{account} and lader=1" })
    int isTeacher(@Param("account") final String account);

    @Update({ "update tb_user set name=#{name}, class_id=(select class_id from tb_class where class_name=#{className}),tele_number=#{teleNumber} where user_id=#{userID}" })
    int updateInformation(final User user);

    @Select({ "select class_name from tb_class" })
    List<String> getClasses();

    @ResultMap({ "toUser" })
    @Select({ "select * from vi_user where tele_number=#{teleNumber} limit 1" })
    User getUserByTeleNumber(@Param("teleNumber") final String teleNumber);

    @ResultMap({ "toUser" })
    @Select({ "select * from vi_user where account=#{account} and password=#{password} limit 1" })
    User getUserByAccount(@Param("account") final String account, @Param("password") final String password);

    @ResultMap({ "toUser" })
    @Select({ "select * from vi_user" })
    List<User> getAllUser();

    @Update({ "update tb_user set name=#{name}, class_id=(select class_id from tb_class where class_name=#{className}),tele_number=#{teleNumber}, password=#{password} where user_id=#{userID}" })
    int updateInformationByLader(final User user);

    @Select({ "select count(*) from tb_record where user_id=#{userID} and pc_num=-1 and out_time is null" })
    int checkUserIsSigned(@Param("userID") final int userID);

    @Select({ "select count(*) from tb_record where user_id=#{userID} and pc_num<>-1 and out_time is null" })
    int checkUserIsSignedComputer(@Param("userID") final int userID);

    @Select({ "select count(*) from tb_user" })
    int countNumberOfUsers();

    @ResultMap({ "toUser" })
    @Select({ "select * from vi_user where user_id in(select user_id from tb_user where lader=1)" })
    List<User> getAllteachers();

    @Select({ "select count(*) from tb_user where lader=1" })
    int countNumberOfTeachers();

    @Insert({ "insert into tb_online (userID) values (#{userID})" })
    int userAddOnline(@Param("userID") final int userID);

    @Insert({ "insert into tb_online (userID,computerID) values (#{userID},#{computerID})" })
    int userAddOnlineComputer(final int userID, final int computerID);

    @Select({ "select userID,computerID from tb_online" })
    List<IntInt> getOnlineUsers();

    @Delete({ "delete from tb_online" })
    int deleteOnlineRecord();

    @Delete({ "delete from tb_online where userID=#{userID}" })
    int deleteOneOnlineRecord(@Param("userID") final int userID);

    @Update({ "update tb_user set password=#{newPassword} where account=#{account} and password=#{password}" })
    int userChangePassword(@Param("account") final String account, @Param("password") final String password, @Param("newPassword") final String newPassword);

    @ResultMap({ "toUser" })
    @SelectProvider(type = DynamicSQLFactory.class, method = "UsersScreen")
    List<User> searchUsers(@Param("userID") final int userID, @Param("account") final String account, @Param("className") final String className, @Param("name") final String name, @Param("teleNumber") final String teleNumber);
}